package com.cauli.manage.storage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cauli.manage.storage.model.dto.StorageAddDTO;
import com.cauli.manage.storage.model.dto.StorageDTO;
import com.cauli.manage.storage.model.dto.StorageQueryDTO;
import com.cauli.manage.storage.model.dto.StorageUpdateDTO;
import com.cauli.manage.storage.model.dto.StorageUpdateStatusDTO;
import com.cauli.manage.storage.model.entity.Storage;
import com.cauli.manage.storage.service.StorageService;
import com.cauli.utils.util.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Cauli
 * @date 2023-03-28 12:00
 * @description 存储源 前端控制器
 */
@Api(tags = "存储源")
@ApiSort(6)
@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    private StorageService storageService;

    @ApiOperationSupport(order = 1)
    @ApiOperation( "获取存储源列表")
    @GetMapping("/list")
    public ResultResponse<Storage> listStorage(StorageQueryDTO storageQueryDTO) {
        Page<Storage> storagePage = storageService.getStoragePage(storageQueryDTO);
        return ResultResponse.success().dataList(storagePage.getRecords(), storagePage.getTotal());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation( "根据存储源ID获取详细信息")
    @GetMapping("/{storageId}")
    public ResultResponse<StorageDTO> getStorage(@PathVariable( "storageId") Long storageId) {
        StorageDTO storageDTO = storageService.getStorageById(storageId);
        return ResultResponse.success().data(storageDTO);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation( "新增存储源")
    @PostMapping
    public ResultResponse<Void> addStorage(@Valid @RequestBody StorageAddDTO storageAddDTO) {
        storageService.addStorage(storageAddDTO);
        return ResultResponse.success().message("添加成功");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation( "修改存储源")
    @PutMapping
    public ResultResponse<Void> updateStorage(@Valid @RequestBody StorageUpdateDTO storageUpdateDTO) {
        storageService.updateStorage(storageUpdateDTO);
        return ResultResponse.success().message("修改成功");
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation( "存储源状修改态")
    @PutMapping("/status")
    public ResultResponse<Void> changeStorageStatus(@Valid @RequestBody StorageUpdateStatusDTO storageUpdateStatusDTO) {
        storageService.changeStorageStatus(storageUpdateStatusDTO);
        return ResultResponse.success().message("修改成功");
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation( "删除存储源")
    @DeleteMapping( "/{storageIds}")
    public ResultResponse<Void> deleteStorage(@PathVariable List<Long> storageIds) {
        storageService.deleteStorage(storageIds);
        return ResultResponse.success().message("删除成功");
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation( "获取存储源大小")
    @GetMapping("/getTotalStorageSize/{userId}")
    public ResultResponse<Long> getTotalStorageSize(@PathVariable Long userId) {
        Long totalStorageSize = storageService.getTotalStorageSize(userId);
        return ResultResponse.success().message("获取成功").data(totalStorageSize);
    }
}
